package cn.ac.iscas.action.patent;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

import cn.ac.iscas.service.patent.IPatentPictureService;

@Controller @Scope("prototype")

public class PatentPictureDownloadAction extends ActionSupport{
	/**
	 * 处理文档的下载服务
	 */
	private static final long serialVersionUID = -2937390798778816655L;
	private String fileName;
	private String fileRealName;
	@Resource private IPatentPictureService patentPictureService;
	public void setFileName()
	{
		// 得到请求下载的文件名
		this.fileName = ServletActionContext.getRequest().getParameter("name");
		this.fileRealName = ServletActionContext.getRequest().getParameter("realname");
	}
	public String getFileName() throws UnsupportedEncodingException {
		System.err.println("fileName="+fileName);
//    	fileName = new String(fileName.getBytes(),"utf-8");
		fileName = URLDecoder.decode(fileName,"utf-8");
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getFileRealName()
	{
	return this.fileRealName;
	}
	public void setFileRealName(String fileRealName) {
		this.fileRealName = fileRealName;
	}
	
	public InputStream getDownloadFile()//函数名需要与struts文件中的result参数一致
	{
		this.setFileName();
		
		String realpath = ServletActionContext.getServletContext().getRealPath("/patentPictures");
		String filePath = "/patentPictures/";	
		//从数据库中找出文档放到Inpustream中		
		
		return ServletActionContext.getServletContext().getResourceAsStream(filePath  +fileRealName);
	}
	
	public IPatentPictureService getPatentPictureService() {
		return patentPictureService;
	}
	public void setPatentPictureService(IPatentPictureService patentPictureService) {
		this.patentPictureService = patentPictureService;
	}
	public String execute() throws Exception
	{
		return SUCCESS;
	}

}
